package easyjava.hibernate.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import easyjava.hibernate.bean.Apartment;

public class ApartmentDao {
	
	private Session session;
	
	public ApartmentDao(Session session){
		this.session=session;
	}
	public List<Apartment> list(){
		Query<Apartment> query =session.createQuery("from Apartment",Apartment.class);
		
		return query.getResultList();
	}
    public Apartment getApartmentById(int id){
    	return session.get(Apartment.class, id);
    }
	public void insert(Apartment apartment) {
		Transaction tx = session.beginTransaction();
		try {

			session.save(apartment);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
	}
	public void update(Apartment apartment){
		Transaction tx = session.beginTransaction();
		try {

			session.update(apartment);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
		
	}
	public void delete(Apartment apartment){
		Transaction tx = session.beginTransaction();
		try {
			
			session.delete(apartment);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
	}	

}
